<template>
    <audio controls ref="audio" :src="playerUseStore.singUrl" type="mp3" @timeupdate="timeUpdate">
    </audio>
    <div class="player">
        <div class="my-player" :class="fixedFlag ? 'my-player-true' : 'my-player-false'">
            <div class="updown">
                <div class="updown-left">
                    <a href="javascript:;" :class="btnFlag ? 'active' : ''"
                        @click="btnFlag = !btnFlag; fixedFlag = !fixedFlag;"></a>
                </div>
                <div class="updown-right">
                </div>
            </div>
            <div class="bg"></div>
            <div class="hand"></div>
            <div class="wrap">
                <div class="btns">
                    <a href="javascript:;" class="prev" @click="prevIndex"></a>
                    <a href="javascript:;" :class="playerUseStore.flag ? 'play' : 'pause'" @click="ControlFlag"></a>
                    <a href="javascript:;" class="next" @click="nextIndex"></a>
                </div>
                <div class="img">
                    <img v-if="playerUseStore.singMessage.al" :src=playerUseStore.singMessage.al.picUrl alt="">
                    <img v-else src="../../assets/images/default_album.jpg" alt="">
                    <a href=""></a>
                </div>
                <div class="play">
                    <div class="words">
                        <a href="" class="singer-title">{{ playerUseStore.singMessage.name }}</a>
                        <div class="author">
                            <span>
                                <a href="" v-for="(item, index) in playerUseStore.singMessage.ar" :key="index">{{
                                    item['name']
                                }}{{ index !== playerUseStore.singMessage.ar.length - 1 &&
    playerUseStore.singMessage.ar.length > 1 ? '/' : '' }}</a>
                            </span>
                        </div>
                        <a href="javascript:;" class="src"></a>
                    </div>
                    <div class="progressBar">
                        <div class="barbg">
                            <div class="rdy"></div>
                            <div class="cur" ref="cur" :style="`width:${width}%`">
                                <div class="cur-btn" @mousedown="startDrag" @mousemove="drag" @mouseup="endDrag">
                                    <i></i>
                                </div>
                            </div>
                        </div>
                        <div class="time">
                            <em>{{ minutes }}:{{ seconds }}</em> /
                            {{ playerUseStore.singTime.minutes }}:{{ playerUseStore.singTime.seconds }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { watch, ref, nextTick, onMounted } from 'vue';
import playerStore from '@/store/modules/player';
import convertMilliseconds from '@/hooks/convertMilliseconds'
let fixedFlag = ref(false)
let btnFlag = ref(false)
let audio = ref()
let cur = ref()
let defaultTime = ref(0)
let playerUseStore = playerStore()
let width = ref(0)
let minutes: any = ref(0)
let seconds: any = ref(0)
const isDragging = ref(false); // 是否正在拖拽
const initialMouseX = ref(0); // 鼠标按下时的初始 X 坐标
const elementX = ref(0); // 元素的初始 X 坐标
watch(() => playerUseStore.singUrl, () => {
    nextTick(() => {
        if (playerUseStore.singUrl != '') {
            audio.value.play();
            countAudioTime()
            if (!fixedFlag.value) {
                fixedFlag.value = true
                setTimeout(() => {
                    fixedFlag.value = false
                }, 4000)
            }
        }
    })
})
const countAudioTime = async () => {
    while (isNaN(audio.value.duration) || audio.value.duration === Infinity) {
        await new Promise(resolve => setTimeout(resolve, 100));
    }
    playerUseStore.singTime = convertMilliseconds(audio.value.duration * 1000)
}
onMounted(() => {
    minutes.value = convertMilliseconds(defaultTime.value).minutes
    seconds.value = convertMilliseconds(defaultTime.value).seconds
    setTimeout(() => {
        fixedFlag.value = false
    }, 4000)
})
fixedFlag.value = true
const ControlFlag = () => {
    if (playerUseStore.singUrl != '') {
        playerUseStore.flag = !playerUseStore.flag
        if (playerUseStore.flag) {
            audio.value.pause()
        } else {
            audio.value.play();
        }
    }
}
const timeUpdate = (e: any) => {
    defaultTime.value = e.target.currentTime * 1000
    const percentagePlayed = (e.target.currentTime / e.target.duration) * 99;
    width.value = percentagePlayed;
    minutes.value = convertMilliseconds(defaultTime.value).minutes
    seconds.value = convertMilliseconds(defaultTime.value).seconds
}
const prevIndex = () => {
    if (playerUseStore.index - 1 < 0) {
        playerUseStore.index = playerUseStore.alSongList.length - 1
        playerUseStore.getsingMessage(playerUseStore.alSongList[playerUseStore.index].id)
    } else {
        playerUseStore.index = playerUseStore.index - 1
        playerUseStore.getsingMessage(playerUseStore.alSongList[playerUseStore.index].id)
    }
}

const nextIndex = () => {
    if (playerUseStore.index >= playerUseStore.alSongList.length - 1) {
        playerUseStore.index = 0
        playerUseStore.getsingMessage(playerUseStore.alSongList[playerUseStore.index].id)
    } else {
        playerUseStore.index = playerUseStore.index + 1
        playerUseStore.getsingMessage(playerUseStore.alSongList[playerUseStore.index].id)
    }
}
const startDrag = (event: { clientX: number; currentTarget: { offsetLeft: any; }; }) => {
  isDragging.value = true;
  initialMouseX.value = event.clientX;
  elementX.value = event.currentTarget.offsetLeft;
};

const drag = (event: { clientX: number; currentTarget: { style: { left: string; }; }; }) => {
  if (isDragging.value) {
    const deltaX = event.clientX - initialMouseX.value;
    event.currentTarget.style.left = `${elementX.value + deltaX}px`;
  }
};

const endDrag = () => {
  isDragging.value = false;
};

</script>

<style scoped lang="less">
audio {
    opacity: 0;
    height: 0;
}

.player {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 0;
    width: 100%;
    z-index: 9999;

    .my-player {
        position: absolute;
        zoom: 1;
        left: 0;
        width: 100%;
        height: 53px;
        margin: 0 auto;
        top: -7px;
        visibility: visible;
        transition: all .4s;

        &:hover {
            top: -53px;
        }

        &.my-player-true {
            top: -53px;
        }

        &.my-player-false {
            top: -7px;

            &:hover {
                top: -53px;
            }
        }

        .updown {
            position: relative;
            z-index: 11;

            .updown-left {
                position: absolute;
                top: -14px;
                right: 15px;
                width: 52px;
                height: 67px;
                background-image: url(../../assets/images/playbar.png);
                background-position: 0 -380px;

                a {
                    display: block;
                    width: 18px;
                    height: 18px;
                    margin: 6px 0 0 17px;
                    background-image: url(../../assets/images/playbar.png);
                    background-position: -80px -380px;

                    &:hover {
                        background-position: -80px -400px;
                    }

                    &.active {
                        background-position: -100px -380px;

                        &:hover {
                            background-position: -100px -400px;
                        }
                    }
                }
            }

            .updown-right {
                position: absolute;
                top: -1px;
                right: 0;
                width: 15px;
                height: 54px;
                background-image: url(../../assets/images/playbar.png);
                background-position: -52px -393px;
                pointer-events: none;
            }
        }

        .bg {
            background-image: url(../../assets/images/playbar.png);
            height: 53px;
            zoom: 1;
            margin-right: 67px;
            background-position: 0 0;
            background-repeat: repeat-x;
        }

        .hand {
            position: absolute;
            top: -10px;
            width: 100%;
            height: 20px;
            cursor: pointer;
        }

        .wrap {
            position: absolute;
            left: 50%;
            top: 6px;
            z-index: 15;
            width: 980px;
            height: 47px;
            margin: 0 auto;
            margin-left: -497.5px;

            .btns {
                width: 137px;
                padding: 6px 0 0 0;
                box-sizing: border-box;
                float: left;

                &::after {
                    content: '';
                    display: block;
                    clear: both;
                }

                a {
                    display: block;
                    float: left;
                    width: 28px;
                    height: 28px;
                    margin-right: 8px;
                    margin-top: 5px;
                    text-indent: -9999px;
                    background-image: url(../../assets/images/playbar.png);

                    &.prev {
                        background-position: 0 -130px;
                    }

                    &.play {
                        width: 36px;
                        height: 36px;
                        margin-top: 0;
                        background-position: 0 -204px;

                        &:hover {
                            background-position: -40px -204px;
                        }
                    }

                    &.pause {
                        width: 36px;
                        height: 36px;
                        margin-top: 0;
                        background-position: 0 -165px;

                        &:hover {
                            background-position: -40px -165px;
                        }
                    }

                    &.next {
                        background-position: -80px -130px;
                    }
                }
            }

            .img {
                position: relative;
                margin: 6px 15px 0 0;
                width: 34px;
                height: 34px;
                float: left;

                img {
                    width: 100%;
                    height: 100%;
                }

                a {
                    position: absolute;
                    top: 0px;
                    left: 0px;
                    display: block;
                    width: 34px;
                    height: 35px;
                    background-position: 0 -80px;
                    background-image: url(../../assets/images/playbar.png);
                }
            }

            .play {
                width: 581px;
                position: relative;
                float: left;

                .words {
                    height: 28px;
                    overflow: hidden;
                    color: #e8e8e8;
                    text-shadow: 0 1px 0 #171717;
                    line-height: 28px;

                    .singer-title {
                        max-width: 300px;
                        color: #e8e8e8;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        word-wrap: normal;
                        text-shadow: 0 1px 0 #171717;
                        line-height: 28px;
                        float: left;
                        font-size: 12px;

                        &:hover {
                            text-decoration: underline;
                        }
                    }

                    .author {
                        max-width: 220px;
                        margin-left: 15px;
                        color: #9b9b9b;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        word-wrap: normal;
                        font-size: 12px;
                        float: left;

                        a {
                            color: #9b9b9b;

                            &:hover {
                                text-decoration: underline;
                            }
                        }
                    }

                    .src {
                        float: left;
                        width: 14px;
                        height: 15px;
                        margin: 7px 0 0 13px;
                        background-image: url(../../assets/images/playbar.png);
                        background-position: -110px -103px;

                        &:hover {
                            background-position: -130px -103px;
                        }
                    }
                }

                .progressBar {
                    width: 466px;
                    position: relative;

                    .barbg {
                        width: 466px;
                        height: 9px;
                        background-position: right 0;
                        background: url(../../assets/images/statbar.png) no-repeat;

                        .rdy {
                            width: 100%;
                            height: 9px;
                            background: url(../../assets/images/statbar.png) no-repeat;
                            background-position: right -30px;
                        }

                        .cur {
                            width: 0;
                            position: absolute;
                            top: 0;
                            left: 0;
                            height: 9px;
                            background: url(../../assets/images/statbar.png) no-repeat;
                            background-position: left -66px;

                            .cur-btn {
                                position: absolute;
                                top: -7px;
                                right: -13px;
                                width: 22px;
                                height: 24px;
                                margin-left: -11px;
                                background: url(../../assets/images/iconall.png);
                                background-position: 0 -250px;

                                &:hover {
                                    background-position: 0 -280px;
                                }
                            }
                        }
                    }

                    .time {
                        position: absolute;
                        top: -3px;
                        right: -84px;
                        color: #797979;
                        text-shadow: 0 1px 0 #121212;
                        font-size: 12px;

                        em {
                            color: #a1a1a1;
                        }
                    }
                }
            }
        }
    }
}
</style>